package com.novelot.plugin.allinone.util;

import com.intellij.icons.AllIcons;
import com.intellij.lang.Language;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.ui.AnimatedIcon;
import com.novelot.plugin.allinone.setting.AppSettingsState;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.gradle.model.data.BuildParticipant;
import org.jetbrains.plugins.gradle.settings.GradleProjectSettings;
import org.jetbrains.plugins.gradle.settings.GradleSettings;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.GrAssignmentExpressionImpl;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.swing.Icon;

public class IdeaApi {


    public static void createJavaClass() {
//        PsiDirectory dir;
//        String name;
//        String templateName;
//        JavaDirectoryService.createClass();
    }

    public static void showPopup() {
        //JBPopupFactory.getInstance().createMessage("气泡").show();
    }

    //https://plugins.jetbrains.com/docs/intellij/work-with-icons-and-images.html#how-to-organize-and-how-to-use-icons
    public static Icon getIcon() {
        return AllIcons.Actions.AddFile;
        //return AllIcons.FileTypes.AddAny;
    }

    public static Icon loadIcon() {
        //Icon Action = IconLoader.getIcon("/icons/myAction.png", MyIcons.class);
        return null;
    }

    public static AnimatedIcon getAnimatedIcon() {
        AnimatedIcon icon = new AnimatedIcon(500, AllIcons.Ide.Macro.Recording_1, AllIcons.Ide.Macro.Recording_2);
        return icon;
    }

    public static void test(Project project) {
//        @NotNull VirtualFile file = new CoreLocalVirtualFile();
//        FileDocumentManager.getInstance().getDocument(file);
//        PsiDocumentManager.getInstance();

//        GradleSettings.MyState state = GradleSettings.getInstance(project).getState();
//        Set<GradleProjectSettings> linkedExternalProjectsSettings = state.getLinkedExternalProjectsSettings();
//        for (GradleProjectSettings linkedExternalProjectsSetting : linkedExternalProjectsSettings) {
//            GradleProjectSettings.CompositeBuild compositeBuild = linkedExternalProjectsSetting.getCompositeBuild();
//            for (BuildParticipant compositeParticipant : compositeBuild.getCompositeParticipants()) {
//            }
//        }
////        GradleProjectSettings.
//
//
//        Logger.v("project.getName()=" + project.getName());
//        Logger.v("project.getProjectFilePath()=" + project.getProjectFilePath());
//        Logger.v("project.getBasePath()=" + project.getBasePath());
//        @NotNull PsiFile[] psiFiles = FilenameIndex.getFilesByName(project, "settings.gradle", GlobalSearchScope.allScope(project));
//        for (PsiFile psiFile : psiFiles) {
//            Logger.v("psiFile.getFileType()=" + psiFile.getFileType());
//            Logger.v("psiFile.getName()=" + psiFile.getName());
//            Logger.v("psiFile.getLanguage()=" + psiFile.getLanguage());
//        }


        String hanisdkDir = AppSettingsState.getInstance().hanisdkDir;
        @NotNull Path path = Paths.get(hanisdkDir + File.separator + "settings.gradle");
        @NotNull VirtualFile vfile = VirtualFileManager.getInstance().findFileByNioPath(path);
        Document document = FileDocumentManager.getInstance().getDocument(vfile);
//        Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
//        Document document = editor.getDocument();
        PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
        Language selectedLanguage = psiFile.getLanguage();
        FileViewProvider viewProvider = psiFile.getViewProvider();


        for (PsiElement e = psiFile.getFirstChild(); e != null; e = e.getNextSibling()) {
            if (isValid(e)) {
                if ("gradle.ext.dev = true".equals(e.getText())) {
                    Logger.v("element.text=" + e.getText() + ",instance of " + e.getClass().getSimpleName());
                    if (e instanceof GrAssignmentExpression) {
                        Logger.d("lvalue=" + ((GrAssignmentExpression) e).getLValue());
                        Logger.d("rvalue=" + ((GrAssignmentExpression) e).getRValue());

                    }
                    GrStatement statement = GroovyPsiElementFactory.getInstance(project).createStatementFromText("gradle.ext.dev = false");
                    e.replace(statement);
                    //
                    Logger.v("replace后,element.text=" + e.getText() + ",instance of " + e.getClass().getSimpleName());
                }
            }
        }
    }

    private static boolean isValid(PsiElement psiElement) {
        return !(psiElement instanceof PsiWhiteSpace);
    }
}
